<template>
  
  <div class="login_container">
    <div class="stars">
    <div class="star" ref="star" v-for="(item,index) in starsCount" :key="index"></div>
  </div>
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <el-form
        ref="loginFormRef"
        label-width="80px"
        class="from"
        :model="loginFrom"
        :rules="loginRules"
      >
        <el-form-item label="用户名" prop="user">
          <el-input
            v-model="loginFrom.name"
            prefix-icon="el-icon-user-solid"
            type="text"
            class="input"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginFrom.password"
            prefix-icon="el-icon-lock"
            class="input"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="button">
          <el-button type="success" @click="loginTo">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      starsCount: 800,
      distance: 800,
      loginFrom: {
        name: "oliver",
        password: "123456"
      },
      loginRules: {
        user: [
          { required: false, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    let _this = this;
    let _starList = document.getElementsByClassName("star");
    let starArr = Array.prototype.slice.call(_starList);
    let starList = this.$refs.star;

    // 遍历添加样式
    starArr.forEach(item => {
      var s = 0.2 + Math.random() * 1;
      var thisDistance = _this.distance + Math.random() * 300;
      item.style.transformOrigin = `0 0 ${thisDistance}px`;
      item.style.transform = `translate3d(0,0,-${thisDistance}px) rotateY(${Math.random() *
        360}deg) rotateX(${Math.random() * -50}deg) scale(${s},${s})`;
    });
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    loginTo() {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return;
        if (this.loginFrom.name.length < 5) {
          return this.$message({
            message: "登录失败",
            type: "error",
            duration: 2000
          });
        }
        this.$message({
          message: "登录成功",
          type: "success",
          duration: 2000
        });
        this.$router.push("/home");
        window.sessionStorage.setItem("token", "admin");
        //this.axios.post("/login",this.loginFrom);
      });
    }
  }
};
</script>

<style scoped>


body {
  background: radial-gradient(
    200% 100% at bottom center,
    #f7f7b6,
    #e96f92,
    #75517d,
    #1b2947
  );
  background: radial-gradient(
    220% 105% at top center,
    #1b2947 10%,
    #75517d 40%,
    #e96f92 65%,
    #f7f7b6
  );
  background-attachment: fixed;
  overflow: hidden;
}
@keyframes rotate {
  0% {
    transform: perspective(400px) rotateZ(20deg) rotateX(-40deg) rotateY(0);
  }
  100% {
    transform: perspective(400px) rotateZ(20deg) rotateX(-40deg)
      rotateY(-360deg);
  }
}
.stars {
  transform: perspective(500px);
  transform-style: preserve-3d;
  position: absolute;
  bottom: 0;
  perspective-origin: 50% 100%;
  left: 50%;
  animation: rotate 90s infinite linear;
}
.star {
  width: 2px;
  height: 2px;
  background: #f7f7b6;
  position: absolute;
  top: 0;
  left: 0;
  backface-visibility: hidden;
}

.login_container {
  background: black;
  opacity: 0.8;
  height: 100%;
  border-radius: 10px;
}

.login_box {
  width: 550px;
  height: 350px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.avatar_box {
  height: 130px;
  width: 130px;
  padding: 10px;
  margin-top: -55%;
}

.avatar_box img {
  width: 100%;
  height: 100%;
}

.from {
  position: absolute;
  margin-top: 25px;
}

.from .input {
  width: 330px;
}

.button {
  display: flex;
  justify-content: flex-end;
}

</style>
